<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>新闻列表</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="format-detection" content="telephone=no">
    <link rel="stylesheet" type="text/css" href="/static/js/layui/css/layui.css" media="all">
    <link rel="stylesheet" type="text/css" href="http://cdn.bootcss.com/bootstrap/3.3.7/css/bootstrap.min.css"
          media="all">
    <link rel="stylesheet" type="text/css" href="/static/css/global.css" media="all">
    <link rel="stylesheet" type="text/css" href="/static/css/personal.css" media="all">
</head>
<body>
<section class="layui-larry-box">
    <div class="larry-personal">
        <div class="layui-tab">
            <blockquote class="layui-elem-quote mylog-info-tit">
                <ul class="layui-tab-title">
                    <li class="layui-btn layui-this"><i class="layui-icon">&#xe60a;</i><span id="title-span"></span></li>
                </ul>
            </blockquote>
            <div class="larry-separate"></div>
            <div class="layui-tab-content larry-personal-body clearfix mylog-info-box">
                <!-- 操作日志 -->

                <div class="layui-tab-item layui-field-box layui-show">
                    <a href="/admin/news/to?type=<%=type%>" class="layui-btn layui-btn-small layui-btn-normal" id="add-btn"><i class="layui-icon">&#xe608;</i>新增文章</a>
                    <a href="javascript:;" class="layui-btn layui-btn-small" id="fresh-btn"><i class="layui-icon">&#xe63d;</i>刷新表格</a>
                    <table class="layui-table table-hover" lay-even="" lay-skin="nob">
                        <thead>
                        <tr>
                            <th>序号</th>
                            <th>标题</th>
                            <th>封面</th>
                            <th>是否置顶</th>
                            <th>更新时间</th>
                            <th>操作</th>
                        </tr>
                        </thead>
                        <tbody id="table_content">
                        </tbody>
                    </table>
                    <div class="larry-table-page clearfix">
                        <div id="page" class="page"></div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</section>
<script type="text/html" id="table_tpl">
    {{#  layui.each(d.data, function(index, item){ }}
    <tr>
        <td>{{index+1}}</td>
        <td>{{item.title}}</td>
        <td><div style="width: 76px;height:76px;" class="cover"><img src="{{item.cover}}" style="display: block;width: 100%;height:100%;"></div></td>
        <td>{{# if(item.top==1){}}置顶中{{# }else{ }}非置顶{{# } }}</td>
        <td>{{item.createTime}}</td>
        <td>
            <a class="layui-btn layui-btn-mini layui-btn-normal" href="/admin/news/to?type={{item.cate}}&id={{item._id}}">编辑</a>
            <a class="layui-btn layui-btn-mini layui-btn-danger js-del-btn" data-id="{{item._id}}">删除</a>
        </td>
    </tr>
    {{#  }); }}
</script>
<script type="text/javascript" src="/static/js/layui/layui.js"></script>
<script type="text/javascript">
    var CFG = {hyxx:'行业信息',gsxw:'公司新闻',rdgz:'热点关注',rsgg:'人事公告'};
    layui.config({
        base: '/static/js/layui/ext/'
    }).use(['jquery', 'layer', 'element', 'laypage','laytpl','common'], function () {
        window.jQuery = window.$ = layui.jquery;
        window.layer = layui.layer;
        var element = layui.element(),
            laypage = layui.laypage,
            laytpl = layui.laytpl,
            common = layui.common;

        var type = '<%=type %>',
            isFirst = true,
            tpl = $("#table_tpl").html(),
            str = CFG[type];
        $("#title-span").text(str);
        var getData = function(page){
            var $load = layer.load();
            common.ajax('/admin/news/data',{type:type,page:page},function(resp){
                layer.close($load);
                var page = resp.data,
                    $table = $("#table_content");
                if(page.count>0){
                    laytpl(tpl).render(page, function(html){
                        $table.html(html);
                    });
                }else{
                    $table.html('<tr><td colspan="6">没有数据...</td></tr>');
                }

                if(isFirst){
                    isFirst = false;
                    laypage({
                        cont: 'page',
                        pages: page.totalPages, //总页数,
                        groups: 5,
                        jump: function (obj, first) {
                            var curr = obj.curr;
                            if (!first) {
                                getData(curr);
                            }
                        }
                    });
                }
            });
        }
        getData(1);

        $("#fresh-btn").click(function(){
            getData(1);
            parent.layer.msg("已刷新数据",{icon:1});
        })

        var showImg = function(that){
            var img = $(that).find('img').attr('src');
            if(!img) return;
            var json = {
                "title": "封面查看",
                "id": 999,
                "data": [
                    {
                        "alt": "封面查看",
                        "pid": 666,
                        "src": img,
                        "thumb": img
                    }
                ]
            }
            layer.photos({photos:json})
        }
        $('#table_content').on('click','div.cover',function(){
            showImg(this);
        })

        var delNews = function(id){
            common.layerDel('温馨提示','确认删除该条新闻吗？','/admin/news/del',{id:id},function(resp){
                parent.layer.closeAll();
                getData(1);
            })
        }
        $('#table_content').on('click','a.js-del-btn',function(){
            var $that = $(this),
                id = $that.data('id');
            delNews(id);
        })

    });
</script>
</body>
</html>